
package com.yuanmei.model;

import com.yuanmei.common.Utils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.Date;
import java.util.Map;

import io.realm.RealmObject;
//import io.realm.annotations.Ignore;
import io.realm.annotations.PrimaryKey;


public class HistoryDayQuote extends RealmObject implements KLineChartQuote {

    @PrimaryKey
    private int date;

    private float open, close, high, low, volume, amount, mcap, turnover, pchg;

    public int getDate() {
        return this.date;
    }

    public void setDate(int date) {
        this.date = date;
    }

    public float getOpen() {
        return this.open;
    }

    public void setOpen(float open) {
        this.open = open;
    }

    public float getHigh() {
        return this.high;
    }

    public void setHigh(float high) {
        this.high = high;
    }

    public float getLow() {
        return this.low;
    }

    public void setLow(float low) {
        this.low = low;
    }

    public float getClose() {
        return this.close;
    }

    public void setClose(float close) {
        this.close = close;
    }

    public float getVolume() {
        return this.volume;
    }

    public void setVolume(float volume) {
        this.volume = volume;
    }

    public float getAmount() {
        return this.amount;
    }

    public void setAmount(float amount) {
        this.amount = amount;
    }

    public float getTurnover() {
        return this.turnover;
    }

    public void setTurnover(float turnover) {
        this.turnover = turnover;
    }

    public float getMcap() {
        return this.mcap;
    }

    public void setMcap(float mcap) {
        this.mcap = mcap;
    }

    public float getPchg() {
        return this.pchg;
    }

    public void setPchg(float pchg) {
        this.pchg = pchg;
    }

    public boolean isQualified() {
        return (this.close > 0.0f) && (this.volume > 0.0f) && (this.open > 0.0f)
                && (this.high > 0.0f) && (this.low > 0.0f);
    }

    @Override
    public String getQuoteDate() {
        Date dt = Utils.getDateFromIDate(this.date);
        String weekDay = Utils.getWeekDay(dt);
        return String.valueOf(this.date) + "/" + weekDay;
    }

    @Override
    public int getQuoteIDate() {
        return this.date;
    }

    @Override
    public float getQuoteOpen() {
        return getOpen();
    }
    @Override
    public float getQuoteHigh() {
        return getHigh();
    }

    @Override
    public float getQuoteLow() {
        return getLow();
    }

    @Override
    public float getQuoteClose(){
        return getClose();
    }

    @Override
    public float getQuoteVolume() {
        return getVolume();
    }

    @Override
    public float getQuoteAmount() {
        return getAmount();
    }

    @Override
    public float getQuoteTurnover() {
        return getTurnover();
    }

    @Override
    public float getQuotePchg() {
        return getPchg();
    }

    public HistoryDayQuote() {
        super();
    }

    public HistoryDayQuote(String[] inValues, Map<String, Integer> neFieldMap) {
        this.date = Utils.parseTuShareFormatDate(inValues[neFieldMap.get("date")]);
        this.open = Utils.safeToFloat(inValues[neFieldMap.get("open")]);
        this.close = Utils.safeToFloat(inValues[neFieldMap.get("close")]);
        this.high = Utils.safeToFloat(inValues[neFieldMap.get("high")]);
        this.low = Utils.safeToFloat(inValues[neFieldMap.get("low")]);
        this.volume = Utils.safeToFloat(inValues[neFieldMap.get("volume")]);
        this.amount = Utils.safeToFloat(inValues[neFieldMap.get("amount")]);
        this.pchg = Utils.safeToFloat(inValues[neFieldMap.get("pchg")]);
        //  for stock index
        if (neFieldMap.get("turnover")  < inValues.length) {
            this.turnover = Utils.safeToFloat(inValues[neFieldMap.get("turnover")]);
        } else {
            this.turnover = 0.0f;
        }
        if (neFieldMap.get("mcap")  < inValues.length) {
            this.mcap = Utils.safeToFloat(inValues[neFieldMap.get("mcap")]);
        } else {
            this.mcap = 0.0f;
        }
    }

    public HistoryDayQuote(JSONArray tuShareItem, Map<String, Integer> tsFieldMap, float lastMcap)
        throws JSONException {
        this.date = tuShareItem.getInt(tsFieldMap.get("date"));
        this.open = (float) (tuShareItem.getDouble(tsFieldMap.get("open")));
        this.close = (float) (tuShareItem.getDouble(tsFieldMap.get("close")));
        this.high = (float) (tuShareItem.getDouble(tsFieldMap.get("high")));
        this.low = (float) (tuShareItem.getDouble(tsFieldMap.get("low")));
        this.volume = (float) (tuShareItem.getDouble(tsFieldMap.get("volume"))) * 100.0f;
        this.amount = (float) (tuShareItem.getDouble(tsFieldMap.get("amount"))) * 1000.0f;;
        this.pchg = (float) (tuShareItem.getDouble(tsFieldMap.get("pchg")));
        this.mcap = lastMcap;
        if (this.mcap != 0) {
            this.turnover = this.amount / this.mcap;
        } else {
            this.turnover = 0.0f;
        }
    }

    public HistoryDayQuote(int date, float open, float high, float low, float close,
                           float volume, float pchg, float amount, float turnover) {
        this.date = date;
        this.open = open;
        this.close = close ;
        this.high =  high;
        this.low = low;
        this.volume = volume;
        this.pchg = pchg;
        this.amount = amount;
        this.turnover = turnover;
    }

    public HistoryDayQuote(int date, TodayQuote tq) {
        this.date = date;
        this.open = tq.getOpen();
        this.close = tq.getPrice() ;
        this.high =  tq.getHigh();
        this.low = tq.getLow();
        this.volume = tq.getVolume();
        this.pchg = tq.getPercent();
        this.amount = 0;
        this.turnover = 0;
    }
}
